import pandas as pd





def select_top_3(frequency, begin_date):
    for result_label in ["annualized_shape", "margin",
                         "information_ratio", "calmar_ratio", "kelly_fraction"]:
        df = pd.read_csv(f'{frequency}/{begin_date}/result/backtest_result_heatmap.csv')

        tmp_df = df.dropna(subset=[f"{result_label}"])
        tmp_df.sort_values(by=[f"{result_label}"], ascending=False, inplace=True)
        concat_df = pd.concat([tmp_df[:3], tmp_df[-3:]])

        for colummn in ["annualized_return", "annualized_volatility", "annualized_shape", "turnover", "margin",
                         "fitness", "excess_return", "relative_return", "max_drawdown", "information_ratio",
                         "calmar_ratio", "yearly_count", "winning_rate", "profit_loss_ratio", "kelly_fraction"]:
            concat_df[colummn] = concat_df[colummn].astype(float).apply(lambda x: f"{x:.4f}")
        concat_df.to_csv(f'{frequency}/{begin_date}/top3/{result_label}.csv', index=False)

